package b2.scene
{
	import b2.net.*;
	import b2.util.*;
	import b2.widget.*;
	
	import flash.display.Sprite;
	import flash.events.*;

	public class SceneManager extends Sprite
	{
		static private var instance:SceneManager;
		public var currentScene:BasicScene;
		public var previousScene:BasicScene;
		//场景层
		public var sceneLayer:Sprite;
		//首层（最上层）
		public var topLayer:Sprite;
		//自定义鼠标
		private var customizedMouse:BasicMc;
		 
		public function SceneManager()
		{
			super();
			sceneLayer = new Sprite();
			topLayer = new Sprite();
			addChild(sceneLayer);
			addChild(topLayer);
		}

		static public function getInstance():SceneManager
		{
			if (!instance)
				instance = new SceneManager();
			return instance;
		}
		
		public function loadScene(scene:BasicScene):void
		{
			if (!scene)
				return;
			if (currentScene != scene)
			{
				unloadScene();
				sceneLayer.addChild(scene);
				currentScene = scene;
			} 
		}
		
		public function unloadScene():void
		{
			if (currentScene)
			{
				sceneLayer.removeChild(currentScene);
				previousScene = currentScene;
			}
		}
		
		public function loadPreviousScene():void
		{
			loadScene(previousScene);
		}
		
		
		//使用自定义鼠标
		public function customizeMouse():void
		{
			if (customizedMouse == null)
			{
				var SandGlass:Class = ResourceHelper.getInstance().getClassByName("SandGlass");
				if (SandGlass != null)
				{
					customizedMouse = new BasicMc(SandGlass);
					customizedMouse.mouseEnabled = false;
					customizedMouse.x = this.mouseX;
					customizedMouse.y = this.mouseY;
					stage.addChild(customizedMouse);
					stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
				}
			}
		}
		
		//移动自定义鼠标
		private function onMouseMove(event:MouseEvent):void
		{
			customizedMouse.x = this.mouseX;
			customizedMouse.y = this.mouseY;
			event.updateAfterEvent();
		}
		
		//还原系统鼠标		
		public function restoreSystemMouse():void
		{
			if (customizedMouse != null)
			{
				stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
				stage.removeChild(customizedMouse);
				customizedMouse = null;
			}
		}
	}
}